﻿using Microsoft.Extensions.DependencyInjection;
using Net8.Project.Model.Options;

namespace Net8.Project.Common
{
    public static class AllOptionRegister
    {
        /// <summary>
        /// 注册选项配置，将程序集中的所有实现了IConfigurableOptions接口的类型注册到服务集合中。
        /// </summary>
        /// <param name="services"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void AddAllOptionSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            var assemblies = new[] {
                typeof(IConfigurableOptions).Assembly,
                typeof(ConfigurableOptions).Assembly,
                // 其他需要扫描的程序集
            };
            foreach (var assembly in assemblies)
            {
                foreach (var optionType in assembly.GetTypes().Where(s =>
                    !s.IsInterface && typeof(IConfigurableOptions).IsAssignableFrom(s)))
                {
                    services.AddConfigurableOptions(optionType);
                }
            }
        }
    }
}
